//
//  StrategyForeignAddressViewController.m
//  ITrip
//
//  Created by dllo on 15/5/29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "StrategyForeignAddressViewController.h"
#import "StrategySecondNearbyViewController.h"
@interface StrategyForeignAddressViewController ()<UIAlertViewDelegate>

@end

@implementation StrategyForeignAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImage *icon = [UIImage imageNamed:@"iconfont_return"];
    icon = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStyleDone target:self action:@selector(getBack)];
    self.view.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    NSString *str = @"旅行地";
    title.text = [self.titleName stringByAppendingString:str];
    title.font = [UIFont systemFontOfSize:20];
    title.font = [UIFont boldSystemFontOfSize:25];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;

    // 定义菊花控件
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:activityIndicator];
    activityIndicator.color = [UIColor grayColor];
    activityIndicator.center = self.view.center;
    [activityIndicator release];
    [activityIndicator startAnimating];
    
    NSString *path = [URLPath strategyForeignAddressURL];
    self.arrayData = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *string = [path stringByAppendingFormat:@"%ld.json?per_page=20&page=1", self.numberAddress];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = nil;
    [manager GET:string parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arrayData = [StrategyForeignAddressParser getParser:operation.responseData];
        [self createMyView];
        [activityIndicator stopAnimating];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activityIndicator stopAnimating];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败" message:nil delegate:self cancelButtonTitle:@"重新连接" otherButtonTitles:nil, nil];
        alert.delegate = self;
        [alert show];
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 定义菊花控件
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:activityIndicator];
    activityIndicator.color = [UIColor grayColor];
    activityIndicator.center = self.view.center;
    [activityIndicator release];
    [activityIndicator startAnimating];
    
    NSString *path = [URLPath strategyForeignAddressURL];
    self.arrayData = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *string = [path stringByAppendingFormat:@"%ld.json?per_page=20&page=1", self.numberAddress];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = nil;
    [manager GET:string parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arrayData = [StrategyForeignAddressParser getParser:operation.responseData];
        [self createMyView];
        [activityIndicator stopAnimating];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activityIndicator stopAnimating];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败" message:nil delegate:self cancelButtonTitle:@"重新连接" otherButtonTitles:nil, nil];
        alert.delegate = self;
        [alert show];
    }];
}

- (void)getBack
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createMyView
{
    myForeignAddress = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 10) style:UITableViewStylePlain];
    myForeignAddress.delegate = self;
    myForeignAddress.dataSource = self;
    myForeignAddress.separatorColor = [UIColor clearColor];
    [self.view addSubview:myForeignAddress];
    myForeignAddress.showsVerticalScrollIndicator = NO;
    [myForeignAddress release];
    __block NSInteger num = 2;
    [myForeignAddress addLegendFooterWithRefreshingBlock:^{
        NSString *path = [URLPath strategyForeignAddressURL];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *string = [path stringByAppendingFormat:@"%ld.json?per_page=20&page=%ld", self.numberAddress, num++];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        NSDictionary *params = nil;
        [manager GET:string parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [self.arrayData addObjectsFromArray:[StrategyForeignAddressParser getParser:operation.responseData]];
            [myForeignAddress reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败" message:nil delegate:self cancelButtonTitle:@"重新连接" otherButtonTitles:nil, nil];
            alert.delegate = self;
            [alert show];
        }];

    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    StrategyForeignAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[StrategyForeignAddressTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    StrategyForeignAddressModel *model = [self.arrayData objectAtIndex:indexPath.row];
    [cell setForeignAddressModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    //cell右侧的小图标
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //添加右半部分的横线
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(165, 110 * (indexPath.row ), self.view.frame.size.width - 180, 1)];
    view.backgroundColor = [UIColor grayColor];
    [tableView addSubview:view];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayData.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StrategySecondNearbyViewController *nearbyViewCon = [[StrategySecondNearbyViewController alloc] init];
    StrategyForeignAddressModel *model = [self.arrayData objectAtIndex:indexPath.row];
    nearbyViewCon.numberNearby = model.address_id;
    [self.navigationController pushViewController:nearbyViewCon animated:YES];
    [nearbyViewCon release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
